<template>
  <div id="body">
    <el-card id="tableCrad">
      <div slot="header" class="clearfix">
        <span>资讯管理</span>
      </div>
      <!-- 查询行开始 -->
      <el-row id="btnRow" type="flex" style="width: 100%">
        <el-col :span="20">
          <el-row type="flex">
            <el-tooltip content="资讯标题" placement="top" effect="light">
              <el-input
                class="input"
                v-model="title"
                placeholder="请输入资讯标题"
                clearable
                style="margin-right: 10px; width: 18%"
              ></el-input>
            </el-tooltip>
            <el-tooltip content="作者名称" placement="top" effect="light">
              <el-input
                class="input"
                v-model="author"
                placeholder="请输入作者名称"
                clearable
                style="margin-right: 10px; width: 18%"
              ></el-input>
            </el-tooltip>
            <el-tooltip content="资讯类别" placement="top" effect="light">
              <el-select
                v-model="type"
                @change="getPages"
                placeholder="请选择资讯类别"
                style="margin-right: 10px; width: 18%"
                filterable
              >
                <el-option
                  v-for="item in queryTypeOptions"
                  :key="item.code.toString()"
                  :label="item.title"
                  :value="item.code.toString()"
                >
                </el-option>
              </el-select>
            </el-tooltip>
            <el-tooltip content="资讯状态" placement="top" effect="light">
              <el-select
                v-model="status"
                @change="getPages"
                placeholder="请选择资讯状态"
                style="margin-right: 10px; width: 18%"
              >
                <el-option label="全部" :value="-1"></el-option>
                <el-option label="已发布" :value="2"></el-option>
                <el-option label="草稿" :value="1"></el-option>
                <el-option label="禁看" :value="0"></el-option>
              </el-select>
            </el-tooltip>
            <el-tooltip content="是否需要通知" placement="top" effect="light">
              <el-select
                v-model="alarm"
                @change="getPages"
                placeholder="请选择通知状态"
                style="margin-right: 10px; width: 18%"
              >
                <el-option label="全部" :value="-1"></el-option>
                <el-option label="不通知" :value="0"></el-option>
                <el-option label="需通知" :value="1"></el-option>
              </el-select>
            </el-tooltip>
            <el-button
              @click="getPages"
              icon="el-icon-search"
              circle
            ></el-button>
          </el-row>
        </el-col>
        <el-col :span="2" :offset="2">
          <el-row>
            <el-button
              @click="addOpen"
              type="primary"
              icon="el-icon-plus"
              circle
            ></el-button>
            <el-button
              @click="deleteBatch"
              type="danger"
              icon="el-icon-delete"
              circle
            ></el-button>
          </el-row>
        </el-col>
      </el-row>
      <!-- 查询行结束 -->
      <!-- 表格开始 -->
      <el-table
        ref="multipleTable"
        :data="tableData"
        style="width: 100%"
        :height="tableHeight"
      >
        <el-table-column type="selection"> </el-table-column>
        <el-table-column fixed prop="id" label="编号" width="80" v-if="false">
        </el-table-column>
        <el-table-column
          prop="title"
          label="标题"
          align="center"
          :show-overflow-tooltip="true"
        >
        </el-table-column>
        <el-table-column
          prop="author"
          label="作者"
          align="center"
          :show-overflow-tooltip="true"
        >
        </el-table-column>
        <el-table-column
          prop="intro"
          label="简介"
          align="center"
          :show-overflow-tooltip="true"
        >
        </el-table-column>
        <el-table-column prop="content" label="内容" align="center" width="50">
          <template slot-scope="scope">
            <el-button type="text" @click="watchEssay(scope.row.content)"
              >查看</el-button
            >
          </template>
        </el-table-column>
        <el-table-column prop="cover" label="封面" align="center" width="80">
          <template slot-scope="scope">
            <el-image
              style="height: 50px"
              :src="baseURL + scope.row.cover"
              :preview-src-list="[baseURL + scope.row.cover]"
              v-if="!isStrEmpty(scope.row.cover)"
            >
            </el-image>
            <span v-else>无</span>
          </template>
        </el-table-column>
        <el-table-column
          prop="type"
          label="类别"
          align="center"
          width="120"
          :show-overflow-tooltip="true"
        >
          <template slot-scope="scope">
            <el-popover placement="top-start" trigger="hover">
              <el-tag
                type="info"
                disable-transitions
                v-for="(title, code) in getEssayTypeArrByStr(scope.row.type)"
                :key="code"
                style="margin: 0 6px"
                >{{ title }}</el-tag
              >
              <el-button type="text" slot="reference">查看</el-button>
            </el-popover>
          </template>
        </el-table-column>
        <el-table-column prop="alarm" label="通知" align="center" width="80">
          <template slot-scope="scope">
            <span>{{ scope.row.alarm == 1 ? "是" : "否" }}</span>
          </template>
        </el-table-column>
        <el-table-column
          prop="status"
          label="类别状态"
          align="center"
          width="80"
        >
          <template slot-scope="scope">
            <el-tag
              :type="scope.row.status == 0 ? 'danger' : 'success'"
              disable-transitions
            >
              {{
                scope.row.status == 0
                  ? "禁看"
                  : scope.row.status == 1
                  ? "草稿"
                  : "已发布"
              }}</el-tag
            >
          </template>
        </el-table-column>
        <el-table-column
          prop="utime"
          label="最新修改"
          align="center"
          width="150"
        >
        </el-table-column>
        <el-table-column fixed="right" label="操作" align="center" width="160">
          <template slot-scope="scope">
            <el-button
              @click.native.prevent="editOpen(scope.row)"
              type="text"
              size="small"
            >
              编辑
            </el-button>
            <el-button
              @click.native.prevent="deleteRow(scope.row)"
              type="text"
              style="color: red"
              size="small"
            >
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        id="pageDevice"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="paginationData.currentPage"
        :page-sizes="[10, 20, 50]"
        :page-size="paginationData.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="paginationData.total"
      >
      </el-pagination>
      <!-- 表格结束 -->
    </el-card>
  </div>
</template>

<script>
import { isStrEmpty } from "@/utils/util";
import { baseURL } from "@/api/request";
export default {
  data() {
    return {
      baseURL: baseURL,
      //查询行参数
      title: "",
      author: "",
      type: "-1",
      status: -1,
      alarm: -1,
      queryTypeOptions: [],
      nomalTypeOptions: [],
      //表格高度
      tableHeight: window.innerHeight - 320,
      //分页参数
      tableData: [],
      paginationData: {
        pageSize: 10,
        currentPage: 1,
        total: 50,
      },
    };
  },
  props: {
    switchView: {
      type: Function,
      default: null,
    },
  },
  methods: {
    handleSizeChange(val) {
      this.paginationData.pageSize = val;
      this.getPages();
    },
    handleCurrentChange(val) {
      this.paginationData.currentPage = val;
      this.getPages();
    },
    getPages() {
      //axios请求方式，通过this.request使用分装好的axios实例
      this.request
        .get("/essay/getPage", {
          params: {
            title: this.title,
            author: this.author,
            type: this.type,
            status: this.status,
            currentPage: this.paginationData.currentPage,
            pageSize: this.paginationData.pageSize,
          },
        })
        .then((res) => {
          if (res.code == 200) {
            this.tableData = res.data.records;
            this.paginationData.total = res.data.total;
            this.paginationData.currentPage = res.data.current;
            this.paginationData.pageSize = res.data.size;
          } else if (res.code == 204) {
            this.tableData = []; // 清空表格数据
            this.paginationData.total = 0; // 总记录数置为0
            this.paginationData.currentPage = 1; // 当前页置为第一页
            this.paginationData.pageSize = 10; // 每页大小置为默认值
          } else {
            this.$message({
              message: res.msg,
              type: "error",
            });
          }
        });
    },
    addOpen() {
      if (this.switchView != null) {
        this.switchView("BusWriteEssay", "menu00060009", null);
      }
    },
    editOpen(row) {
      if (this.switchView != null) {
        this.switchView("BusWriteEssay", "menu00060009", row.id);
      }
    },
    deleteRow(row) {
      this.$alert("是否确定删除", "提示", {
        confirmButtonText: "确定",
        callback: (action) => {
          if (action == "confirm") {
            this.request
              .post("/essay/delete", {
                id: row.id,
              })
              .then((res) => {
                if (res.code == 200) {
                  this.$message({
                    message: res.msg,
                    type: "success",
                  });
                  this.getPages();
                } else {
                  this.$message({
                    message: res.msg,
                    type: "error",
                  });
                }
              });
          }
        },
      });
    },
    deleteBatch() {
      var selectedList = this.$refs.multipleTable.selection;
      if (selectedList.length <= 0) {
        this.$message({
          message: "请选择至少一条数据",
          type: "warning",
        });
        return;
      }
      var ids = [];
      for (var i = 0; i < selectedList.length; i++) {
        ids.push(selectedList[i].id);
      }
      this.$alert("是否确定删除", "提示", {
        confirmButtonText: "确定",
        callback: (action) => {
          if (action == "confirm") {
            this.request
              .post("/essay/deleteBatch", {
                ids: JSON.stringify(ids),
              })
              .then((res) => {
                if (res.code == 200) {
                  this.$message({
                    message: res.msg,
                    type: "success",
                  });
                  this.getPages();
                } else {
                  this.$message({
                    message: res.msg,
                    type: "error",
                  });
                }
              });
          }
        },
      });
    },
    getEssayType() {
      this.request
        .get("/essayType/getIndentOptions", {
          params: {
            parent: -1,
            deleted: 0,
          },
        })
        .then((res) => {
          if (res.code == 200) {
            this.queryTypeOptions = res.data;
            if (res.data[0].title == "根节点") {
              res.data[0].title = "全部";
            }
          } else {
            this.$message({
              message: res.msg,
              type: "error",
            });
          }
        });
    },
    watchEssay(content) {
      console.log(content);
      //const params = { id: val };
      //let routeData = this.$router.resolve({
      //  name: baseURL + content,
      //  //query: params,
      //});
      window.open(baseURL + content, "_blank");
    },
    isStrEmpty(str) {
      return isStrEmpty(str);
    },
    async getNormalEssayType() {
      await this.request
        .get("/essayType/getAllOptions", {
          params: {
            deleted: -1,
          },
        })
        .then((res) => {
          if (res.code == 200) {
            this.nomalTypeOptions = res.data;
          } else {
            this.$message({
              message: res.msg,
              type: "error",
            });
          }
        });
    },
    getEssayTypeArrByStr(str) {
      let arr = [];
      if (isStrEmpty(str)) {
        return arr;
      }
      const codeArr = str.split(",");
      for (let i = 0; i < codeArr.length; i++) {
        this.nomalTypeOptions.forEach((item) => {
          if (item.code == codeArr[i]) {
            arr.push(item.title);
          }
        });
      }
      return arr;
    },
  },
  mounted() {
    window.addEventListener("resize", () => {
      this.tableHeight = window.innerHeight - 320;
    });
  },
  async created() {
    //创建
    await this.getNormalEssayType();
    this.getPages();
    this.getEssayType();
  },
};
</script>

<style>
#body {
  display: flex;
  flex-direction: column;
  align-items: center;
}
#tableCrad {
  width: 100%;
}
#btnRow {
  padding-bottom: 10px;
  border-bottom: 1px solid #eee;
}
#pageDevice {
  text-align: right;
}
.text {
  font-size: 14px;
}

.item {
  margin-bottom: 18px;
}
.clearfix:before,
.clearfix:after {
  display: table;
  content: "";
}
.clearfix:after {
  clear: both;
}

.demo-table-expand {
  font-size: 0;
}
.demo-table-expand label {
  width: 90px;
  color: #99a9bf;
}
.demo-table-expand .el-form-item {
  text-align: center;
  margin-bottom: 0;
  width: 24%;
}
</style>